namespace ScrumSprintMonitor.UI.Wpf.Converters
{
	#region #using Directives

	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Windows.Media;

	#endregion

	public class BaseColorToSolidBrushValueHelper : IDisposable
	{
		private readonly Dictionary<Color, Brush> brushCacheDictionary = new Dictionary<Color, Brush>();

		protected Brush GetBrushFromColor(Color color)
		{
			Brush brush;

			if (!this.brushCacheDictionary.TryGetValue(color, out brush))
			{
				brush = new SolidColorBrush(color);
				this.brushCacheDictionary.Add(color, brush);
			}

			return brush;
		}

		#region Implementation of IDisposable

		public void Dispose()
		{
			this.brushCacheDictionary.Values.OfType<IDisposable>().ToList().ForEach(brush => brush.Dispose());
			this.brushCacheDictionary.Clear();
		}

		#endregion
	}
}